//
//  DJUIButtonController.swift
//  dj-learn-Swift
//
//  Created by Sun Jae on 2022/4/24.
//  Copyright © 2022 S.J. All rights reserved.
//

import UIKit
import RxSwift

class DJUIButtonController: DJViewController {
    
    // 创建一个可观察者
    var boolObservable: Observable<Bool>!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.view.backgroundColor = UIColor.gray
        
//        let corView: SJRectCornerView = SJRectCornerView()
//        self.view.addSubview(corView)
//        corView.snp.makeConstraints { (make) in
//            make.left.equalTo(self.view)
//            make.top.equalTo(self.view).offset(30)
//            make.right.equalTo(self.view)
//            make.height.equalTo(100.0)
//        }
//        corView.fillColor = UIColor.red
//        corView.cornerRadii = CGSize(width: 100, height: 30)
//        corView.roundCorners = [UIRectCorner.topLeft, UIRectCorner.topRight]
        
        let textField = UITextField()
        textField.borderStyle = .roundedRect
        self.view.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.top.equalTo(self.view).offset(30)
            make.left.equalTo(self.view).offset(30)
            make.right.equalTo(self.view).offset(-30)
            make.height.equalTo(40)
        }
        
        let rightButton: SJButton = SJButton(type: .custom)
        self.view.addSubview(rightButton)
        rightButton.backgroundColor = UIColor.gray
        rightButton.snp.makeConstraints { (make) in
            make.top.equalTo(textField.snp.bottom).offset(30)
            make.left.equalTo(self.view).offset(30)
            make.right.equalTo(self.view).offset(-30)
            make.height.equalTo(50)
        }
        rightButton.imageSize = CGSize(width: 20, height: 20)
        rightButton.imagePostion = .top
        rightButton.midSpace = 5.0
        rightButton.setTitleColor(UIColor.green, for: .normal)
        rightButton.setImage(UIImage(named: "icon_uilabel"), for: .normal)
        rightButton.setTitle("这是一个按钮", for: .normal)
        rightButton.setTitle("无法点击", for: .selected)
        
        boolObservable = Observable.create({ (observer) -> Disposable in
            let value = UserDefaults.standard.bool(forKey: "TEST_BOOL")
            if value {
                observer.onNext(true)
            } else {
                observer.onNext(false)
            }
            return Disposables.create()
        })
        
        let textVaild = textField.rx.text.orEmpty.map {
            return $0.count > 5
        }.share(replay: 1)
        
        let everythingValid =  Observable.combineLatest(boolObservable, textVaild) {
            $0 && $1
        }.share(replay: 1)
    
        everythingValid.bind(to: rightButton.rx.isSelected).disposed(by: self.disposeBag)
    
        
        rightButton.rx.tap.subscribe(onNext: { event in
            
        })
        

    }
    


}
